﻿using static System.Net.Mime.MediaTypeNames;

namespace Recast
{
    public class CppAnalysis
    {
        public Dictionary<string, TypeModel> Types { get; set; } = new Dictionary<string, TypeModel>();
        public List<string> Defines = new List<string>();

        public void ClassHandler
        (
            ref string text,
            bool isClass,
            bool isStruct,
            string typeName = "",
            string returnName = "",
            string methodName = "",
            string paramsName = "",
            string methodBody = "",
            string ctorBody = "",
            string genericTypeName = "",
            bool isStatic = false,
            bool isDelegateSet = false,
            bool isLnline = false,
            bool isCtor = false,
            bool isDelegate = false,
            bool isGenericType = false,
            bool isVirtual = false
        )
        {
            if (isClass)
                text = text.TrimStart("class".ToCharArray()).Trim();
            if (isStruct)
                text = text.TrimStart("struct".ToCharArray()).Trim();
            TypeModel? type = default;
            int i = 0;
            while (i < text.Length)
            {
                if (text[i] == ':') //继承
                {
                    typeName = text.Substring(0, i).Trim();
                    text = text.Remove(0, i + 1).Trim();
                    GetTypeModel(typeName, out type);
                    type.IsClass = isClass;
                    type.IsStruct = isStruct;
                    if (text.StartsWith("public"))
                        text = text.TrimStart("public".ToCharArray()).Trim();
                    i = text.IndexOf("{");
                    type.inheritType = text.Substring(0, i).Trim();
                    text = text.Remove(0, i).Trim();
                    i = 0;
                }
                if (text[i] == '{')
                {
                    if (i > 0)
                    {
                        typeName = text.Substring(0, i).Trim();
                        text = text.Remove(0, i).Trim();
                        GetTypeModel(typeName, out type);
                        type.IsClass = isClass;
                        type.IsStruct = isStruct;
                    }
                    text = text.TrimStart('{').Trim();
                J1:
                    if (text.StartsWith("static"))
                    {
                        isStatic = true;
                        text = text.Remove(0, "static".Length).Trim();
                    }
                    if (text.StartsWith("inline"))
                    {
                        isLnline = true;
                        text = text.Remove(0, "inline".Length).Trim();
                    }
                    if (text.StartsWith("const"))
                    {
                        text = text.Remove(0, "const".Length).Trim();
                    }
                    if (text.StartsWith("unsigned"))
                    {
                        text = text.Remove(0, "unsigned".Length).Trim();
                    }
                    if (text.StartsWith("typedef"))
                    {
                        text = text.Remove(0, "typedef".Length).Trim();
                    }
                    if (text.StartsWith("struct"))
                    {
                        //StructHandler(ref text);
                        ClassHandler(ref text, false, true);
                    }
                    i = 0;
                    while (i < text.Length)
                    {
                        if (text[i] == '(') //先找到(说明是方法
                        {
                            var mtdAndRtn = text.Substring(0, i).Trim();
                            text = text.Remove(0, i + 1).Trim();
                            if (mtdAndRtn.StartsWith("static"))
                            {
                                isStatic = true;
                                mtdAndRtn = mtdAndRtn.Remove(0, "static".Length).Trim();
                            }
                            if (mtdAndRtn.StartsWith("inline"))
                            {
                                isLnline = true;
                                mtdAndRtn = mtdAndRtn.Remove(0, "inline".Length).Trim();
                            }
                            if (mtdAndRtn.StartsWith("virtual"))
                            {
                                isVirtual = true;
                                mtdAndRtn = mtdAndRtn.Remove(0, "virtual".Length).Trim();
                            }
                            i = mtdAndRtn.IndexOf("*"); //返回值包含指针
                            if (i == -1)
                                i = mtdAndRtn.IndexOf(" "); //有返回值
                            if (i > 0)
                            {
                                returnName = mtdAndRtn.Substring(0, i + 1).Trim();
                                methodName = mtdAndRtn.Substring(i + 1, mtdAndRtn.Length - i - 1).Trim();
                            }
                            else //构造函数
                            {
                                methodName = mtdAndRtn;
                            }
                            i = 0;
                            while (i < text.Length)
                            {
                                if (text[i] == ')') //方法结束
                                {
                                    var @params = text.Substring(0, i + 1).Trim(); //参数
                                    text = text.Remove(0, i + 1).Trim();
                                    i = 0;
                                    while (i < @params.Length)
                                    {
                                        if (@params[i] == ',') //多个参数
                                        {
                                            var param = @params.Substring(0, i).Trim();
                                            @params = @params.Remove(0, i + 1).Trim();
                                            ParamsHandler(param, ref i, ref paramsName);
                                            i = 0;
                                        }
                                        if (@params[i] == ')') //最后一个参数
                                        {
                                            if (i == 0) //无参方法
                                                break;
                                            var param = @params.Substring(0, i).Trim();
                                            @params = @params.Remove(0, i + 1).Trim();
                                            ParamsHandler(param, ref i, ref paramsName);
                                            break;
                                        }
                                        i++;
                                    }
                                    i = 0;
                                    while (i < text.Length)
                                    {
                                        if (text[i] == ';') //不实现的方法
                                        {
                                            text = text.Remove(0, i + 1).Trim();
                                            isCtor = methodName == typeName;
                                            var Name = $"public {(isDelegate ? "delegate" : (isStatic ? "static" : ""))} {returnName} {methodName}{(isGenericType ? $"<{genericTypeName}>" : "")}{(isDelegateSet ? "" : $"({paramsName})")}";
                                            Name = Name.Replace("  ", " ").Trim();
                                            if (!type.Methods.TryGetValue(Name, out var method))
                                            {
                                                type.Methods.Add(Name, method = new MethodModel()
                                                {
                                                    methodName = methodName,
                                                    paramsName = paramsName,
                                                    returnName = returnName,
                                                    methodBody = methodBody,
                                                    isStatic = isStatic,
                                                    isDelegateSet = isDelegateSet,
                                                    isLnline = isLnline,
                                                    isCtor = isCtor,
                                                    ctorBody = ctorBody,
                                                    isDelegate = isDelegate,
                                                    isGenericType = isGenericType,
                                                    genericTypeName = genericTypeName,
                                                    isVirtual = isVirtual,
                                                });
                                            }
                                            i = 0;
                                            paramsName = "";
                                            goto J1;
                                        }
                                        if (text[i] == '{') //实现的方法
                                        {
                                            if (i > 0) //构造函数初始化赋值
                                            {
                                                ctorBody = $"/*{text.Substring(0, i).Trim()}*/";
                                                text = text.Remove(0, i).Trim();
                                            }
                                            i = 0;
                                            int n = 0;
                                            while (i < text.Length)
                                            {
                                                if (text[i] == '{') //函数体中途发生{}操作
                                                {
                                                    n++;
                                                }
                                                if (text[i] == '}')
                                                {
                                                    if (--n <= 0)
                                                    {
                                                        methodBody = text.Substring(0, i + 1).Trim();
                                                        text = text.Remove(0, i + 1).Trim();
                                                        var Name = $"public {(isDelegate ? "delegate" : (isStatic ? "static" : ""))} {returnName} {methodName}{(isGenericType ? $"<{genericTypeName}>" : "")}{(isDelegateSet ? "" : $"({paramsName})")}";
                                                        Name = Name.Replace("  ", " ").Trim();
                                                        if (!type.Methods.TryGetValue(Name, out var method))
                                                        {
                                                            type.Methods.Add(Name, method = new MethodModel()
                                                            {
                                                                methodName = methodName,
                                                                paramsName = paramsName,
                                                                returnName = returnName,
                                                                methodBody = methodBody,
                                                                isStatic = isStatic,
                                                                isDelegateSet = isDelegateSet,
                                                                isLnline = isLnline,
                                                                isCtor = isCtor,
                                                                ctorBody = ctorBody,
                                                                isDelegate = isDelegate,
                                                                isGenericType = isGenericType,
                                                                genericTypeName = genericTypeName,
                                                                isVirtual = isVirtual,
                                                            });
                                                        }
                                                        method.methodBody = methodBody;
                                                        i = 0;
                                                        paramsName = "";
                                                        goto J1;
                                                    }
                                                }
                                                i++;
                                            }
                                        }
                                        i++;
                                    }
                                }
                                i++;
                            }

                            break;
                        }
                        else if (text[i] == ';') //找到这个了还没有(说明是字段
                        {
                            var fields = text.Substring(0, i).Trim();
                            text = text.Remove(0, i + 1).Trim();
                            var item = ParamsHandler(fields, ref i, ref paramsName);
                            while ((i = item.name.IndexOf(",")) >= 0)
                            {
                                var fieldName = item.name.Substring(0, i).Trim();
                                item.name = item.name.Remove(0, i + 1).Trim();
                                type.Fields.Add(fieldName, item.type);
                            }
                            type.Fields.Add(item.name, item.type);
                            i = 0;
                            paramsName = "";
                            goto J1;
                        }
                        else if (text[i] == '}' & text[i + 1] == ';')
                        {
                            text = text.Remove(0, i + 2).Trim();
                            goto J2;
                        }
                        i++;
                    }
                }
                i++;
            }
        J2:;
        }

        public void GlobalMethodHandler
        (
            ref string text,
            string typeName = "",
            string returnName = "",
            string methodName = "",
            string paramsName = "",
            string methodBody = "",
            string ctorBody = "",
            string genericTypeName = "",
            bool isStatic = false,
            bool isDelegateSet = false,
            bool isLnline = false,
            bool isCtor = false,
            bool isDelegate = false,
            bool isGenericType = false,
            bool isVirtual = false
        )
        {
            int i = 0;
            GetTypeModel("ClassGlobal", out TypeModel type);
            type.IsClass = true;
            if (text.StartsWith("{")) //无效方法
            {
                int n = 0;
                while (i < text.Length)
                {
                    if (text[i] == '{') //处理 void*(dtAllocFunc)(int size,  dtAllocHint hint);
                    {
                        n++;
                    }
                    if (text[i] == '}')
                    {
                        if (--n <= 0)
                        {
                            text = text.Remove(0, i + 1).Trim();
                            goto J2;
                        }
                    }
                    i++;
                }
            }
            if (text.StartsWith("static"))
            {
                isStatic = true;
                text = text.Remove(0, "static".Length).Trim();
            }
            if (text.StartsWith("inline"))
            {
                isLnline = true;
                text = text.Remove(0, "inline".Length).Trim();
            }
            if (text.StartsWith("const"))
            {
                text = text.Remove(0, "const".Length).Trim();
            }
            if (text.StartsWith("unsigned"))
            {
                text = text.Remove(0, "unsigned".Length).Trim();
            }
            if (text.StartsWith("template"))
            {
                isGenericType = true;
                text = text.Remove(0, "template".Length + 1).Trim();
                if (text.StartsWith("class"))
                {
                    text = text.Remove(0, "class".Length).Trim();
                    int index1 = text.IndexOf(">");
                    genericTypeName = text.Substring(0, index1);
                    text = text.Remove(0, index1 + 1).Trim();
                    if (text.StartsWith("class")) //泛型中又定义了class关键字
                    {
                        ClassHandler(ref text, true, false, isGenericType: true);
                        return;
                    }
                }
                if (text.StartsWith("typename"))
                {
                    text = text.Remove(0, "typename".Length).Trim();
                    int index1 = text.IndexOf(">");
                    genericTypeName = text.Substring(0, index1);
                    text = text.Remove(0, index1 + 1).Trim();
                    if (text.StartsWith("class"))
                    {
                        ClassHandler(ref text, true, false, isGenericType: true);
                        return;
                    }
                }
            }
            if (text.StartsWith("typedef"))
            {
                isDelegate = true;
                text = text.Remove(0, "typedef".Length).Trim();
                int n = 0;
                while (i < text.Length)
                {
                    if (text[i] == '(') //处理 void*(dtAllocFunc)(int size,  dtAllocHint hint);
                    {
                        n++;
                    }
                    if (text[i] == ';') //静态变量 宏修饰
                    {
                        var define = text.Substring(0, i);
                        text = text.Remove(0, i + 1).Trim().Insert(i, " ");
                        var item = ParamsHandler(define, ref i, ref paramsName);
                        switch (item.type)
                        {
                            case "byte":
                                item.type = "System.Byte";
                                break;
                            case "sbyte":
                                item.type = "System.SByte";
                                break;
                            case "short":
                                item.type = "System.Int16";
                                break;
                            case "ushort":
                                item.type = "System.UInt16";
                                break;
                            case "char":
                                item.type = "System.Char";
                                break;
                            case "int":
                                item.type = "System.Int32";
                                break;
                            case "uint":
                                item.type = "System.UInt32";
                                break;
                            case "float":
                                item.type = "System.Single";
                                break;
                            case "long":
                                item.type = "System.Int64";
                                break;
                            case "ulong":
                                item.type = "System.UInt64";
                                break;
                            case "uint64_t":
                                item.type = "System.UInt64";
                                break;
                            case "double":
                                item.type = "System.Double";
                                break;
                            case "intptr_t":
                                item.type = "System.IntPtr";
                                break;
                            default:
                                //throw new Exception("未实现!");
                                goto J2;
                        }
                        type.Defines.Add($"using {item.name} = {item.type};");
                        goto J2;
                    }
                    if (text[i] == ')' & text[i + 1] == ';')
                    {
                        if (n >= 2)
                        {
                            i = text.IndexOf("(");
                            text = text.Remove(i, 1).Trim().Insert(i, " ");
                            i = text.IndexOf(")");
                            text = text.Remove(i, 1).Trim().Insert(i, " ");
                            n = 0;
                            i = 0;
                            continue;
                        }
                        break;
                    }
                    i++;
                }
            }
            i = 0;
            while (i < text.Length)
            {
            J1:
                if (text[i] == '(') //先找到(说明是方法   !!!!!先找到(也不一定是方法, 而是变量默认赋值转换(x)
                {
                    var mtdAndRtn = text.Substring(0, i).Trim();
                    text = text.Remove(0, i + 1).Trim();
                    if (mtdAndRtn.StartsWith("inline"))
                    {
                        isLnline = true;
                        mtdAndRtn = mtdAndRtn.Remove(0, "inline".Length).Trim();
                    }
                    i = mtdAndRtn.IndexOf("*"); //返回值包含指针
                    if (i == -1)
                        i = mtdAndRtn.IndexOf(" "); //有返回值
                    if (i > 0)
                    {
                        returnName = mtdAndRtn.Substring(0, i + 1).Trim();
                        methodName = mtdAndRtn.Substring(i + 1, mtdAndRtn.Length - i - 1).Trim();
                    }
                    else //构造函数
                    {
                        methodName = mtdAndRtn;
                    }
                    i = methodName.IndexOf("::"); //类的实现部分cpp
                    if (i >= 0)
                    {
                        typeName = methodName.Substring(0, i).Trim();
                        methodName = methodName.Remove(0, i + 2).Trim();
                        GetTypeModel(typeName, out type);
                        type.IsClass = true;
                    }
                    i = 0;
                    while (i < text.Length)
                    {
                        if (text[i] == ')') //方法结束
                        {
                            var @params = text.Substring(0, i + 1).Trim(); //参数
                            text = text.Remove(0, i + 1).Trim();
                            i = 0;
                            while (i < @params.Length)
                            {
                                if (@params[i] == ',') //多个参数
                                {
                                    var param = @params.Substring(0, i).Trim();
                                    @params = @params.Remove(0, i + 1).Trim();
                                    ParamsHandler(param, ref i, ref paramsName);
                                    i = 0;
                                }
                                if (@params[i] == ')') //最后一个参数
                                {
                                    if (i == 0) //无参方法
                                        break;
                                    var param = @params.Substring(0, i).Trim();
                                    @params = @params.Remove(0, i + 1).Trim();
                                    ParamsHandler(param, ref i, ref paramsName);
                                    break;
                                }
                                i++;
                            }
                            i = 0;
                            while (i < text.Length)
                            {
                                if (text[i] == ';') //不实现的方法
                                {
                                    text = text.Remove(0, i + 1).Trim();
                                    isCtor = methodName == typeName;
                                    var Name = $"public {(isDelegate ? "delegate" : (isStatic ? "static" : ""))} {returnName} {methodName}{(isGenericType ? $"<{genericTypeName}>" : "")}{(isDelegateSet ? "" : $"({paramsName})")}";
                                    Name = Name.Replace("  ", " ").Trim();
                                    if (!type.Methods.ContainsKey(Name))
                                    {
                                        type.Methods.Add(Name, new MethodModel()
                                        {
                                            methodName = methodName,
                                            paramsName = paramsName,
                                            returnName = returnName,
                                            methodBody = methodBody,
                                            isStatic = isStatic,
                                            isDelegateSet = isDelegateSet,
                                            isLnline = isLnline,
                                            isCtor = isCtor,
                                            ctorBody = ctorBody,
                                            isDelegate = isDelegate,
                                            isGenericType = isGenericType,
                                            genericTypeName = genericTypeName,
                                            isVirtual = isVirtual,
                                        });
                                    }
                                    i = 0;
                                    paramsName = "";
                                    goto J2;
                                }
                                if (text[i] == '{') //实现的方法
                                {
                                    if (i > 0) //构造函数初始化赋值
                                    {
                                        ctorBody = $"/*{text.Substring(0, i).Trim()}*/";
                                        text = text.Remove(0, i).Trim();
                                    }
                                    i = 0;
                                    int n = 0;
                                    while (i < text.Length)
                                    {
                                        if (text[i] == '{') //函数体中途发生{}操作
                                        {
                                            n++;
                                        }
                                        if (text[i] == '}')
                                        {
                                            if (--n <= 0)
                                            {
                                                methodBody = text.Substring(0, i + 1).Trim();
                                                text = text.Remove(0, i + 1).Trim();
                                                var Name = $"public {(isDelegate ? "delegate" : (isStatic ? "static" : ""))} {returnName} {methodName}{(isGenericType ? $"<{genericTypeName}>" : "")}{(isDelegateSet ? "" : $"({paramsName})")}";
                                                Name = Name.Replace("  ", " ").Trim();
                                                if (!type.Methods.TryGetValue(Name, out var method))
                                                {
                                                    type.Methods.Add(Name, method = new MethodModel()
                                                    {
                                                        methodName = methodName,
                                                        paramsName = paramsName,
                                                        returnName = returnName,
                                                        methodBody = methodBody,
                                                        isStatic = isStatic,
                                                        isDelegateSet = isDelegateSet,
                                                        isLnline = isLnline,
                                                        isCtor = isCtor,
                                                        ctorBody = ctorBody,
                                                        isDelegate = isDelegate,
                                                        isGenericType = isGenericType,
                                                        genericTypeName = genericTypeName,
                                                        isVirtual = isVirtual,
                                                    });
                                                }
                                                method.methodBody = methodBody;
                                                i = 0;
                                                paramsName = "";
                                                goto J2;
                                            }
                                        }
                                        i++;
                                    }
                                }
                                i++;
                            }
                        }
                        i++;
                    }
                    break;
                }
                else if (text[i] == ';') //找到这个了还没有(说明是字段
                {
                    var fields = text.Substring(0, i).Trim();
                    text = text.Remove(0, i + 1).Trim();
                    var item = ParamsHandler(fields, ref i, ref paramsName);
                    while ((i = item.name.IndexOf(",")) >= 0)
                    {
                        var fieldName = item.name.Substring(0, i).Trim();
                        item.name = item.name.Remove(0, i + 1).Trim();
                        type.Fields.Add(fieldName, item.type);
                    }
                    type.Fields.Add(item.name, item.type);
                    i = 0;
                    paramsName = "";
                    goto J2; //字段完成后紧跟着就是结构类型
                }
                else if (text[i] == '=') //字段定义时赋值 dtNodeIndex DT_null_IDX = (dtNodeIndex)~0;
                {
                    i = 0;
                    while (i < text.Length)
                    {
                        if (text[i] == ';')
                        {
                            goto J1; //去上面;处理字段
                        }
                        i++;
                    }
                }
                else if (text[i] == '}' & text[i + 1] == ';')
                {
                    text = text.Remove(0, i + 2).Trim();
                    goto J2;
                }
                i++;
            }
        J2:;
        }

        /*
        public void StructHandler
        (
            ref string text,
            bool isClass,
            bool isStruct,
            string typeName = "",
            string returnName = "",
            string methodName = "",
            string paramsName = "",
            string methodBody = "",
            string ctorBody = "",
            string genericTypeName = "",
            bool isStatic = false,
            bool isDelegateSet = false,
            bool isLnline = false,
            bool isCtor = false,
            bool isDelegate = false,
            bool isGenericType = false,
            bool isVirtual = false
        )
        {
            if (isClass)
                text = text.TrimStart("class".ToCharArray()).Trim();
            if (isStruct)
                text = text.TrimStart("struct".ToCharArray()).Trim();
            TypeModel? type = null;
            int i = 0;
            while (i < text.Length)
            {
                if (text[i] == ':') //继承
                {
                    typeName = text.Substring(0, i).Trim();
                    text = text.Remove(0, i + 1).Trim();
                    GetTypeModel(typeName, out type);
                    type.IsStruct = isStruct;
                    type.IsClass = isClass;
                    if (text.StartsWith("public"))
                        text = text.TrimStart("public".ToCharArray()).Trim();
                    i = text.IndexOf("{");
                    type.inheritType = text.Substring(0, i).Trim();
                    text = text.Remove(0, i).Trim();
                    i = 0;
                }
                if (text[i] == '{')
                {
                    if (i > 0)
                    {
                        typeName = text.Substring(0, i).Trim();
                        text = text.Remove(0, i).Trim();
                        GetTypeModel(typeName, out type);
                        type.IsStruct = isStruct;
                        type.IsClass = isClass;
                    }
                    text = text.TrimStart('{').Trim();
                    i = 0;
                    while (i < text.Length)
                    {
                    J1:
                        if (text[i] == '(') //先找到(说明是方法
                        {
                            var mtdAndRtn = text.Substring(0, i).Trim();
                            text = text.Remove(0, i + 1).Trim();
                            var item = ParamsHandler(mtdAndRtn, ref i, ref paramsName);
                            returnName = item.type;
                            methodName = item.name;
                            paramsName = "";
                            i = 0;
                            while (i < text.Length)
                            {
                                if (text[i] == ')') //方法结束
                                {
                                    var @params = text.Substring(0, i + 1).Trim(); //参数
                                    text = text.Remove(0, i + 1).Trim();
                                    i = 0;
                                    while (i < @params.Length)
                                    {
                                        if (@params[i] == ',') //多个参数
                                        {
                                            var param = @params.Substring(0, i).Trim();
                                            @params = @params.Remove(0, i + 1).Trim();
                                            ParamsHandler(param, ref i, ref paramsName);
                                            i = 0;
                                        }
                                        if (@params[i] == ')') //最后一个参数
                                        {
                                            if (i == 0) //无参方法
                                                break;
                                            var param = @params.Substring(0, i).Trim();
                                            @params = @params.Remove(0, i + 1).Trim();
                                            ParamsHandler(param, ref i, ref paramsName);
                                            break;
                                        }
                                        i++;
                                    }
                                    i = 0;
                                    while (i < text.Length)
                                    {
                                        if (text[i] == ';') //不实现的方法
                                        {
                                            text = text.Remove(0, i + 1).Trim();
                                            isCtor = methodName == typeName;
                                            var Name = $"public {(isDelegate ? "delegate" : (isStatic ? "static" : ""))} {returnName} {methodName}{(isGenericType ? $"<{genericTypeName}>" : "")}{(isDelegateSet ? "" : $"({paramsName})")}";
                                            Name = Name.Replace("  ", " ").Trim();
                                            if (!type.Methods.TryGetValue(Name, out var method))
                                            {
                                                type.Methods.Add(Name, method = new MethodModel()
                                                {
                                                    methodName = methodName,
                                                    paramsName = paramsName,
                                                    returnName = returnName,
                                                    methodBody = methodBody,
                                                    isStatic = isStatic,
                                                    isDelegateSet = isDelegateSet,
                                                    isLnline = isLnline,
                                                    isCtor = isCtor,
                                                    ctorBody = ctorBody,
                                                    isDelegate = isDelegate,
                                                    isGenericType = isGenericType,
                                                    genericTypeName = genericTypeName,
                                                    isVirtual = isVirtual,
                                                });
                                            }
                                            i = 0;
                                            paramsName = "";
                                            goto J1;
                                        }
                                        if (text[i] == '{') //实现的方法
                                        {
                                            if (i > 0) //构造函数初始化赋值
                                            {
                                                ctorBody = text.Substring(0, i).Trim();
                                                text = text.Remove(0, i).Trim();
                                            }
                                            i = 0;
                                            int n = 0;
                                            while (i < text.Length)
                                            {
                                                if (text[i] == '{') //函数体中途发生{}操作
                                                {
                                                    n++;
                                                }
                                                if (text[i] == '}')
                                                {
                                                    if (--n <= 0)
                                                    {
                                                        methodBody = text.Substring(0, i + 1).Trim();
                                                        text = text.Remove(0, i + 1).Trim();
                                                        var Name = $"public {(isDelegate ? "delegate" : (isStatic ? "static" : ""))} {returnName} {methodName}{(isGenericType ? $"<{genericTypeName}>" : "")}{(isDelegateSet ? "" : $"({paramsName})")}";
                                                        Name = Name.Replace("  ", " ").Trim();
                                                        if (!type.Methods.TryGetValue(Name, out var method))
                                                        {
                                                            type.Methods.Add(Name, method = new MethodModel()
                                                            {
                                                                methodName = methodName,
                                                                paramsName = paramsName,
                                                                returnName = returnName,
                                                                methodBody = methodBody,
                                                                isStatic = isStatic,
                                                                isDelegateSet = isDelegateSet,
                                                                isLnline = isLnline,
                                                                isCtor = isCtor,
                                                                ctorBody = ctorBody,
                                                                isDelegate = isDelegate,
                                                                isGenericType = isGenericType,
                                                                genericTypeName = genericTypeName,
                                                                isVirtual = isVirtual,
                                                            });
                                                        }
                                                        method.methodBody = methodBody;
                                                        i = 0;
                                                        paramsName = "";
                                                        goto J1;
                                                    }
                                                }
                                                i++;
                                            }
                                        }
                                        i++;
                                    }
                                }
                                i++;
                            }

                            break;
                        }
                        else if (text[i] == ';') //找到这个了还没有(说明是字段
                        {
                            var fields = text.Substring(0, i).Trim();
                            text = text.Remove(0, i + 1).Trim();
                            var item = ParamsHandler(fields, ref i, ref paramsName);
                            while ((i = item.name.IndexOf(",")) >= 0)
                            {
                                var fieldName = item.name.Substring(0, i).Trim();
                                item.name = item.name.Remove(0, i + 1).Trim();
                                type.Fields.Add(fieldName, item.type);
                            }
                            type.Fields.Add(item.name, item.type);
                            i = 0;
                            paramsName = "";
                            goto J1;
                        }
                        else if (text[i] == '}' & text[i + 1] == ';')
                        {
                            text = text.Remove(0, i + 2).Trim();
                            goto J2;
                        }
                        i++;
                    }
                }
                i++;
            }
        J2:;
        }
        */

        public void EnumHandler(ref string text)
        {
            text = text.TrimStart("enum".ToCharArray()).Trim();
            int i = 0;
            while (i < text.Length)
            {
                if (text[i] == '{')
                {
                    var typeName = text.Substring(0, i).Trim();
                    text = text.Remove(0, i + 1).Trim();
                    GetTypeModel(typeName, out var type);
                    type.IsEnum = true;
                    i = 0;
                    while (i < text.Length)
                    {
                        if (text[i] == ',')
                        {
                            var fieldName = text.Substring(0, i).Trim();
                            text = text.Remove(0, i + 1).Trim();
                            type.Fields.Add(fieldName, "");
                            i = 0;
                        }
                        if (text[i] == '}' & text[i + 1] == ';')
                        {
                            var fieldName = text.Substring(0, i).Trim();
                            text = text.Remove(0, i + 2).Trim();
                            type.Fields.Add(fieldName, "");
                            return;
                        }
                        i++;
                    }
                }
                i++;
            }
        }

        private void GetTypeModel(string typeName, out TypeModel type)
        {
            if (!Types.TryGetValue(typeName, out type))
                Types.Add(typeName, type = new TypeModel(typeName) { Defines = Defines });
        }

        private (string type, string name) ParamsHandler(string param, ref int i, ref string paramsName)
        {
            if (param.StartsWith("static"))
            {
                param = param.Remove(0, "static".Length).Trim();
            }
            if (param.StartsWith("inline"))
            {
                param = param.Remove(0, "inline".Length).Trim();
            }
            if (param.StartsWith("const"))
            {
                param = param.Remove(0, "const".Length).Trim();
            }
            if (param.StartsWith("unsigned"))
            {
                param = param.Remove(0, "unsigned".Length).Trim();
            }
            if (param.StartsWith("typedef"))
            {
                param = param.Remove(0, "typedef".Length).Trim();
            }
            if (param.StartsWith("struct"))
            {
                param = param.Remove(0, "struct".Length).Trim();
            }
            if (param.StartsWith("class"))
            {
                param = param.Remove(0, "class".Length).Trim();
            }
            var isRef = param.Contains('&');
            if (isRef)
                param = param.Replace("&", "");
            i = param.IndexOf("*"); //返回值包含指针
            if (i == -1)
                i = param.IndexOf(" "); //有返回值
            if (i > 0)
            {
                var paramType = param.Substring(0, i + 1).Trim();
                var paramName = param.Substring(i + 1, param.Length - i - 1).Trim();
                if (paramsName.Length > 0)
                    paramsName += ", ";
                paramsName += $"{(isRef ? "ref" : "")} {paramType} {paramName}";
                return (paramType, paramName);
            }
            else //构造函数
            {
                paramsName += $"{(isRef ? "ref " : "")} {param} @{param}";
                return (param, "@" + param);
            }
        }

        public void Sort(ref string text, params string[] chars)
        {
            foreach (var cha in chars)
            {
                int idx = 0;
                while (idx < text.Length)
                {
                    idx = text.IndexOf(cha, idx);
                    if (idx == -1)
                        break;
                    if (idx - 1 < 0)
                        break;
                    while (text[idx - 1] == ' ')
                    {
                        idx--; //因为移除后会倒塌字符, 所以需要--
                        var test = text.Substring(idx);
                        text = text.Remove(idx, 1);
                        if (idx - 1 < 0)
                            break;
                    }
                    if (idx + cha.Length >= text.Length) //多个字符时判断
                        break;
                    while (text[idx + cha.Length] == ' ')
                    {
                        text = text.Remove(idx + cha.Length, 1); //后面的移除不会倒塌到前面的判断
                        if (idx + cha.Length >= text.Length)
                            break;
                    }
                    idx++;
                }
            }
        }
    }
}
